﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BattleShip_MongoDB
{
    public class Ship
    {
        private int iSize;
        private Point TopLeft;
        private bool bDirection;    /*0: Horizon, 1: Vertical*/
        private List<bool> bCellShipStatusList;
        private bool bShipStatus;

        public Ship(int iSize, Point TopLeft, bool bDirection)
        {
            this.iSize = iSize;
            this.TopLeft = TopLeft;
            this.bDirection = bDirection;

            bCellShipStatusList = new List<bool>();

            for (int i = 0; i < iSize; i++)
            {
                bCellShipStatusList.Add(true);
            }
            
            bShipStatus = true;
        }

        public bool ShotedAt(Point point)
        {
            int iDashtemp = 0;
            bool bIsNeedUpdateStatus = false;
            bool blShipStatus = true;

            if (bDirection)
            {
                if (TopLeft.X == point.X)
                {
                    iDashtemp = point.Y - TopLeft.Y;
                    if (iDashtemp >= 0 && iDashtemp < iSize)
                    {
                        if (bCellShipStatusList[iDashtemp])
                        {
                            bCellShipStatusList[iDashtemp] = false;
                            bIsNeedUpdateStatus = true;
                        }
                    }
                }
            }
            else
            {
                if (TopLeft.Y == point.Y)
                {
                    iDashtemp = point.X - TopLeft.X;
                    if (iDashtemp >= 0 && iDashtemp < iSize)
                    {
                        if (bCellShipStatusList[iDashtemp])
                        {
                            bCellShipStatusList[iDashtemp] = false;
                            bIsNeedUpdateStatus = true;
                        }
                    }
                }
            }

            if (bIsNeedUpdateStatus)
            {
                blShipStatus = UpdateShipStatus();
            }

            return blShipStatus;
        }

        private bool UpdateShipStatus()
        {
            int iAliveCell = 0;

            for (int i = 0; i < iSize; i++)
            {
                if (bCellShipStatusList[i] == true)
                {
                    iAliveCell++;
                }
            }

            if (iAliveCell == 0)
            {
                bShipStatus = false;
            }

            return bShipStatus;
        }

        public bool GetShipStatus()
        {
            return bShipStatus;
        }

        public int GetShipSize()
        {
            return iSize;
        }

        public bool GetShipDirection()
        {
            return bDirection;
        }

        public Point GetShipTopLeftPosition()
        {
            return TopLeft;
        }
    }
}
